from langgraph.graph import StateGraph, START, END
from graph.state import AgentState
from graph.graph import fetch_data_node, parse_input_node, generate_response_node
from langgraph.graph import StateGraph, END

builder = StateGraph(AgentState)
builder.add_node("parse_input", parse_input_node)
builder.add_node("fetch_data", fetch_data_node)
builder.add_node("generate_response", generate_response_node) 

builder.set_entry_point("parse_input")
builder.add_edge("parse_input", "fetch_data")
builder.add_edge("fetch_data", "generate_response") 
builder.add_edge("generate_response", END)

graph = builder.compile()

# 初始化对话历史
message_history = []

print("英雄联盟上分助手已启动! 输入'退出'或'quit'结束对话。")
while True:
    user_input = input("\n您: ")
    
    if user_input.lower() in ["退出", "quit", "exit"]:
        print("助手: 再见！祝您上分顺利！")
        break
        
    # 运行Graph处理用户输入
    initial_state = {
        "user_input": user_input,
        "message_history": message_history  # 传入当前的对话历史
    }
    
    final_state = graph.invoke(initial_state)
    
    # 打印助手的响应
    print(f"助手: {final_state.get('response', '抱歉，我遇到了一些问题。')}")
    
    # 更新对话历史
    message_history = final_state.get("message_history", [])